/*
 * 提取 JS 渲染结果
 */

const puppeteer = require('puppeteer');

const timestart = new Date().getTime();
// 启动 chrome 
(async() => {  
	// 默认启动内嵌的 chrome
	const browser = await puppeteer.launch(
                {args: ['--no-sandbox'],
		 ignoreHTTPSErrors: true}
        );
	// open webpage
	const page = await browser.newPage();
	//await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36');
        //await page.setRequestInterception(true);                           
        //page.on('request', interceptedRequest => {
	//	const url = interceptedRequest.url().toLowerCase();
	//	for (name of ['.jpg', '.jpeg', '.png', '.gif']){           
        //                if (url.endsWith(name)){
	//			// 终止请求
	//			interceptedRequest.abort();
        //                        return ;
	//		}
	//	}
	//	interceptedRequest.continue();
	//});
	await page.goto('https://weibo.com/', {waitUntil: 'domcontentloaded'});
	//await sleep(5000);
	// 等待 dom 被渲染处理
	page.waitForFunction(()=>{
		const query = '//div[h2/text()="微博新鲜事"]';
		const box = document.evaluate( query, document, null, XPathResult.ANY_TYPE, null ).iterateNext();
		return box != null;
	}, {
		// 最多等待10秒
		timeout: 10000
	}).then(async()=>{
		// 前面都是铺垫, 这里开始提取页面
		let result = [];
		const box = await page.$x('//div[h2/text()="微博新鲜事"]');
		const items = await box[0].$x('.//div[@class="UG_contents"]/div[@class="UG_list_d"]');
		await (async()=>{
			for (item of items){
				// img
				const imgHdl = await item.$x('.//img/@src');
				const img = await textValue(imgHdl[0], page);
				// title
				const titleHdl = await item.$x('.//h3/a/text()');
				const title = await textValue(titleHdl[0], page);
				// url
				const urlHdl = await item.$x('.//h3/a/@href');
				const url = await textValue(urlHdl[0], page);
				result.push({title, img, url})
			}
		})();
		// print
		console.log(result);
		await browser.close();
		
		const timeend = new Date().getTime();
		console.log(timestart, timeend, timeend - timestart);
	}, e=>{
		// 等待 js 渲染超时
		console.log('找不到指定区域', e);
	});	
})();

async function textValue(jsHandle){
	const text = await jsHandle.executionContext().evaluate(e=> e.textContent, jsHandle);
	return text;
}
function sleep(time){
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve();
        }, time);
    })
}
